package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.BankAccountService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements BankAccountService {
    /**
     * 新增银行账号信息
     */
    @Override
    public void addBank(BankAccountResDTO bankAccountReqDTO) {
        BankAccount bankAccount = BeanUtil.copyProperties(bankAccountReqDTO, BankAccount.class);
        Long userId = UserContext.currentUserId();
        bankAccount.setId(userId);
        save(bankAccount);
    }

    /**
     * 更新银行账号信息
     */
    @Override
    public void updateBank(BankAccountResDTO bankAccountReqDTO) {
        Long userId = UserContext.currentUserId();
        BankAccount bankAccount = BeanUtil.copyProperties(bankAccountReqDTO, BankAccount.class);
        bankAccount.setUpdateTime(new Date());
        bankAccount.setId(userId);
        updateById(bankAccount);
    }

    /**
     * 获取当前用户银行账号
     */
    @Override
    public BankAccount getUserBankAccount() {
        Long userId = UserContext.currentUserId();
        return baseMapper.selectById(userId);
    }

    /**
     * 机构端新增银行账号信息
     */
    @Override
    public BankAccount addBankAgency(BankAccountResDTO bankAccountReqDTO) {
        BankAccount bankAccount = BeanUtil.copyProperties(bankAccountReqDTO, BankAccount.class);
        Long userId = UserContext.currentUserId();
        bankAccount.setId(userId);
        save(bankAccount);
        return bankAccount;
    }

    /**
     * 机构端更新银行账号信息
     */
    @Override
    public BankAccount updateBankAgency(BankAccountResDTO bankAccountReqDTO) {
        Long userId = UserContext.currentUserId();
        BankAccount bankAccount = BeanUtil.copyProperties(bankAccountReqDTO, BankAccount.class);
        bankAccount.setUpdateTime(new Date());
        bankAccount.setId(userId);
        updateById(bankAccount);
        return bankAccount;
    }
}
